<?php
namespace src;
use classlib\input;
use src\logic\Cadmin;
use classlib\core as C;
use classlib\core\controller;
class Cdistrict extends controller {
	public function main() {
		$view   = C\view::inst('extjs');
		$userid = Cadmin::chkaccess($view);
		$view->assign_by_ref('title', Cadmin::$name);
		$view->display('core/district.ext.js');
	}
	
	public function doshare() {
		$view  = C\view::inst('json');
		Cadmin::islogin($view);
		list($start, $limit) = Cadmin::offset();
		$query = input::post('query');
		$where = array();
		if ($query) {
			$where = array(array('name', '%'.$query.'%', \classlib\orm::OP_LIKE));
		} else {
			$where = array('pid'=>0);
		}
		$total = C\model::load('main.sys.district')->get_total($where);
		$list  = array();
		if ($total > 0) {
			$field = 'id, pid, name';
			$list  = C\model::load('main.sys.district')->get_list($start, $limit, $where, $field);
			foreach ($list as &$item) {
				$temp = $item;
				while ($temp['pid'] > 0) {
					$data = C\model::load('main.sys.district')->get_one($temp['pid']);
					if ($data) {
						$item['name'] = $data['name'].$item['name'];
						$temp         = $data;
						continue;
					}
					break;
				}
			}
		}
		$view->assign_by_ref('total', $total);
		$view->assign_by_ref('dataset', $list);
		$view->display();
	}
	
	public function dotree() {
		$view = C\view::inst('json');
		Cadmin::chkaccess($view);
		$field= '*,name as text';
		$sort = array('id'=>\classlib\orm::ASC);
		$where= array('pid'=>(int)input::get_post('node'));
		$orm  = C\model::load('main.sys.district');
		$list = $orm->get_list(0, -1, $where, $field, $sort);
		foreach ($list as &$item) {
			$nlen = $orm->get_total(array('pid'=>$item['id']));
			$item['leaf']    = ($nlen > 0)? false:true;
			$item['checked'] = '';
		}
		ob_end_clean();
		$tree = json_encode($list); 
		echo $tree;
	}
	
	/**
	 * @desc    删除模块功能，只能删除子模块
	 * @doc     删除功能
	 * @access  需要授权
	 */
	public function doremove() {
		$view  = C\view::inst('json');
		Cadmin::chkaccess($view);
		$ids   = input::post('ids');
		$ids   = explode(',', $ids);
		$ids   = array_filter($ids, 'is_numeric');
		$lang  = C\lang::inst('adm');
		if (!$ids) {
			$view->assign_error('1', $lang->get('common', '40001'));
		}
		foreach ($ids as $id) {
			if (!is_numeric($id) || $id < 1) {
				continue;
			}
			$nlen = C\model::load('main.sys.district')->get_total(array('pid'=>$id));
			if ($nlen > 0) {
				continue;
			}
			C\model::load('main.sys.district')->delete($id);
		}
		$view->display();
	}
	
	/**
	 * @desc    编辑中国地理结构信息
	 * @doc     编辑中国地理
	 * @access  需要授权
	 */
	public function dosave() {
		$view   = C\view::inst('json');
		Cadmin::chkaccess($view);
		$data   = array();
		$lang   = C\lang::inst('adm');
		$filter = new \classlib\validator\auditor();
		$filter->lang($lang);
		try {
			$data['name']  = $filter->reset('name')->required()->minlen(1)->maxlen(255)->run();
			$data['pid']   = $filter->reset('pid')->required()->numeric()->min(0)->run();
			$data['rank']  = $filter->reset('rank')->required()->numeric()->run();
		} catch (\classlib\validator\AuditorException $e) {
			$view->assign_error($e->getField(), $e->getMessage());
		} catch (\Exception $e) {
			$view->assign_error($e->getCode(), $e->getMessage());
		}
		if ($data['pid'] > 0) {
			$item = C\model::load('main.sys.district')->get_one($data['pid']);
			if (!$item) {
				$view->assign_error('pid', $lang->get('common', '40004'));
			}
		}
		$id     = (int)input::get_post('id');
		if($id > 0) {
			C\model::load('main.sys.district')->save($id, $data);
		} else {
			C\model::load('main.sys.district')->new_one($data);
		}
		$view->display();
	}
}